package hotel;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.GregorianCalendar;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfWriter;

public class CreaSchedaClientePDF {
	public static GregorianCalendar gc=new GregorianCalendar();
	public static int giorno=gc.get(GregorianCalendar.DATE);
	public static int mese=gc.get(GregorianCalendar.MONTH)+1;
	public static int anno=gc.get(GregorianCalendar.YEAR);
	public static String oggi=giorno+"-"+mese+"-"+anno;
	public GraficaHotel g;
	private static ImageIcon iconav = new ImageIcon("Risorse/iconav.png");
	
	public CreaSchedaClientePDF(GraficaHotel g,String key) throws DocumentException, SQLException, MalformedURLException, IOException, ClassNotFoundException{
		this.g=g;
		creaDocumento(key);
	}

	private void creaDocumento(String key) throws DocumentException, SQLException, MalformedURLException, ClassNotFoundException{
		MyResultSet rs=g.database.querySelect("SELECT * FROM "+g.username+".CLIENTI WHERE numerodidocumento='"+key+"'");
		rs.next();
		String nome=rs.getString(2);
		String cognome=rs.getString(3);
		String dataDiNascita=rs.getString(4);
		String comuneNascita=rs.getString(5);
		String provNascita=rs.getString(6);
		String residenza=rs.getString(7);
		String provResidenza=rs.getString(8);
		String tipoDoc=rs.getString(9);
		String rilasciatoDa=rs.getString(11);
		String dataRilascio=rs.getString(12);
		String imm=rs.getString(14);
		String email=rs.getString(15);
		JFileChooser fc=new JFileChooser();
		fc.setApproveButtonText("Crea PDF");
		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		File file;
		String indirizzo;
		if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
			file = fc.getSelectedFile();
			indirizzo = file.getPath()+"/"+nome+"-"+cognome+"-"+oggi+".pdf";
		}else
			return;
		Document document = new Document();
		try {
			PdfWriter.getInstance(document, new FileOutputStream(indirizzo));
		} catch (FileNotFoundException e1) {}
		document.open();
		document.addTitle("Cliente "+key);
		document.addSubject("Cliente Hotel");
		document.addKeywords("clienti,hotel,"+key);
		document.addAuthor("GestionaleHotel");
		Chapter chapter = new Chapter(new Paragraph("Informazioni"), 1);
		Paragraph sectionParagraph = new Paragraph("Cliente "+nome+" "+cognome);
		Section section = chapter.addSection(sectionParagraph);
		section.add(new Paragraph("Nome: "+nome));
		section.add(new Paragraph("Cognome: "+cognome));
		section.add(new Paragraph("Data di Nascita: "+dataDiNascita));
		section.add(new Paragraph("Comune di Nascita: "+comuneNascita));
		section.add(new Paragraph("Provincia di Nascita: "+provNascita));
		section.add(new Paragraph("Residenza: "+residenza));
		section.add(new Paragraph("Provincia di Residenza: "+provResidenza));
		section.add(new Paragraph("Tipo di Documento: "+tipoDoc));
		section.add(new Paragraph("Numero di Documento: "+key));
		section.add(new Paragraph("Rilasciato da: "+rilasciatoDa));
		section.add(new Paragraph("Data di Rilascio: "+dataRilascio));
		section.add(new Paragraph("Email: "+email));
		Image jpeg;
		try {
			jpeg = Image.getInstance(imm);
			section.add(jpeg);
		} catch (IOException e) {}
		document.add(section);
		document.close();
		JOptionPane.showMessageDialog(null,"Pdf Esportato con successo!","Ok!", JOptionPane.INFORMATION_MESSAGE, iconav);
	}
}
